<?php
require 'bigfoot.php';
require 'changeset.php';

/**
 * ChangeSetRollback
 *
 * @package default
 * @author Keith Alexander
 **/
class ChangeSetRollback
{
	/**
	 * uri
	 *
	 * @var string
	 **/
	var $uri;
	
	/**
	 * CHANGESET_NS
	 *
	 * @var string
	 **/
	const CHANGESET_NS = 'http://purl.org/vocab/changeset/schema#'; 
	
	/**
	 * store
	 *
	 * @var object
	 **/
	var $store;
	
	/**
	 * errors
	 *
	 * @var array
	 **/
	var $errors;
	
	function __construct($store_uri, $changeset_uri)
	{
		$this->uri = $changeset_uri;
		$this->store = new BigfootStore($store_uri);
	}
	
	function build_changeset()
	{
		$rdf = $this->get_following_changesets();
		$parser = new ARC_rdfxml_parser(array());
		$triples = $parser->parse_data($rdf);
		$additions = array();
		$removals = array();
		foreach($triples as $t)
		{
			if($t['p']==$this->CHANGESET_NS.'addition') $removals[]=$t;
			else if($t['p']==$this->CHANGESET_NS.'removal') $additions[] = $t;
		}
		$resources = triples_to_resources($triples, array('cs'=>$this->CHANGESET_NS));
		$changesets = find_by_type($resources,$this->CHANGESET_NS.'ChangeSet');
	}
	
	/**
	 * get_following_changesets()
	 *
	 * @return rdf/xml of following changesets
	 * @author Keith Alexander
	 **/
	function get_following_changesets()
	{
		$query = <<<_SPARQL_
		DESCRIBE ?following <{$this->uri}> 
		WHERE 
		{
			?following <{$this->CHANGESET_NS}subjectOfChange> <{$this->uri}>;
					   <{$this->CHANGESET_NS}createdDate> ?f_date .
			<{$this->uri}> <{$this->CHANGESET_NS}createdDate> ?cs_date .
			FILTER(?f_date > ?cs_date)
		}
_SPARQL_;
		$response = $this->store->sparql_service->graph($query);
		if($response->status_code=='200') return $response->body;
		else
		{
			$this->errors[] = $response->body;
			return false; 
		}
	}
	
	
} // END class 


?>